package com.rui.shangXueTang.queue;

/**
 * @author : [锐神NO.3]
 * @version : [v1.0]
 * @className : ArrayQueue
 * @createTime : [2022/2/14 20:03]
 * @description : [数组实现队列]
 */

@SuppressWarnings("all")
public class ArrayQueue {

    private int[] arr;

    private int maxSize;

    private int frontPoint;

    private int rearPoint;

    /**
     * 初始化队列
     * @param maxSize 队列的最大容量
     */
    public ArrayQueue(int maxSize){
        this.maxSize = maxSize;
        arr = new int[maxSize];
        frontPoint = -1;
        rearPoint = -1;
    }

    /**
     * 判断当前队列是否已满
     */
    public boolean isFull(){
        return rearPoint == maxSize - 1;
    }

    /**
     * 判断当前队列是否为空
     */
    public boolean isEmpty(){
        return frontPoint == rearPoint;
    }

    /**
     * 添加元素进入队列
     */
    public void add(int value){
        //判断是否已满
        if(isFull()){
            System.out.println("队列已满,无法添加");
            return;
        }

        rearPoint++;
        arr[rearPoint] = value;
    }

    /**
     * 从队列中取出元素
     */
    public int poll(){
        //判断是否为空
        if(isEmpty()){
            System.out.println("队列为空,无法取出");
            return -1;
        }

        frontPoint++;
        return arr[frontPoint];
    }

    /**
     * 查看队列中的元素
     */
    public void findQueue(){
        if(isEmpty()){
            System.out.println("队列为空,无法查看");
            return;
        }

        for(int i = frontPoint + 1; i <= rearPoint; i++){
            System.out.print(arr[i] + "\t");
        }
    }

    /**
     * 查看队首元素
     */
    public int findQueueHead(){
        if(isEmpty()){
            System.out.println("队列为空,无法查看");
            return -1;
        }
        return arr[frontPoint + 1];
    }
}
